ggplot2

Basic bar chart

ggplot(diamonds, aes(x=cut)) + 
  geom_bar()

Flipped bar chart coordinates

ggplot(diamonds, aes(x=cut)) + 
  geom_bar() +
  coord_flip()

Stacked Bar

ggplot(diamonds, aes(x=cut, colour = color, fill = color)) +
  geom_bar()

Unstacked Bar

ggplot(diamonds, aes(x=cut, colour = color, fill = color)) +
  geom_bar(position = "dodge")

Basic Scatter Plot

ggplot(mtcars, aes(x = hp, y = mpg)) + 
  geom_point()

Show possible correlation

ggplot(mtcars, aes(x = hp, y = mpg)) + 
  geom_point() + 
  geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Linear

ggplot(mtcars, aes(x = hp, y = mpg)) + 
  geom_point() + 
  geom_smooth(color = "red", method = "lm")
## `geom_smooth()` using formula 'y ~ x'

Histogram

ggplot(iris, aes(x = Petal.Length, fill = Species)) +
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Geom Density

ggplot(iris, aes(x = Sepal.Length)) +
  geom_density(fill = "green", alpha = .6)

Geom Density by Species

ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density(alpha = .6)

Box Plot

ggplot(starwars, aes(x = species, y = height)) + 
  geom_boxplot()
## Warning: Removed 6 rows containing non-finite values (stat_boxplot).

Violin

ggplot(starwars, aes(x = gender, y = mass, fill = gender)) +
  geom_violin()
## Warning: Removed 28 rows containing non-finite values (stat_ydensity).

Violin with quantiles

ggplot(starwars, aes(x = gender, y = mass, fill = gender)) +
  geom_violin(draw_quantiles = c(.25, .5, .75))
## Warning: Removed 28 rows containing non-finite values (stat_ydensity).
## Warning in regularize.values(x, y, ties, missing(ties), na.rm = na.rm):
## collapsing to unique 'x' values

Timeseries

ggplot(airquality, aes(x=Date, y = Wind)) +
  geom_line() +
  geom_point()

Multiple Lines Method 1

## Multiple Time Series
ggplot(airquality, aes(x=Date)) +
  geom_line(aes(y = Wind), color = "blue") +
  geom_line(aes(y = Temp), color = "red")

Multiple Lines Method 2

## Alternative
p.aq <- airquality %>%
   pivot_longer(-Date) %>%
  filter(name %in% c("Wind", "Temp"))

ggplot(p.aq, aes(x=Date, y=value, color = name)) +
  geom_point() +
  geom_line()

Adding Labels

ggplot(p.aq, aes(x=Date, y=value, color = name)) +
  geom_point() +
  geom_line() +
  xlab("Date") +
  ylab("Wind: mph / Temp: degrees F") +
  labs(color = "Measurement", caption = "Source: National Weather Service") +
  ggtitle("Observed Wind and Temperature", subtitle = "New York City 1973")

Colors

ggplot(starwars, aes(x = gender, y = mass, fill = gender)) +
  geom_violin(draw_quantiles = c(.25, .5, .75)) +
  scale_fill_brewer(palette = "Set1")
## Warning: Removed 28 rows containing non-finite values (stat_ydensity).
## Warning in regularize.values(x, y, ties, missing(ties), na.rm = na.rm):
## collapsing to unique 'x' values

Custom Themes

# Customt ggplot theme
custom_theme <- theme(
                      line = element_line(colour = "black", size = 0.5, 
                                          linetype = 1, lineend = "butt"), 
                      rect = element_rect(fill = "white", colour = "black",
                                          size = 0.5, linetype = 1),
                      text = element_text(family = "", face = "plain",
                                          colour = "black", size = 11,
                                          lineheight = 0.9,  hjust = 0.5,
                                          vjust = 0.5, angle = 0, 
                                          margin = margin(), debug = FALSE), 
                      
                      axis.line = element_blank(), 
                      axis.text = element_text(size = rel(0.8), colour = "grey30"),
                      axis.text.x = element_text(margin = margin(t = 0.8*5.5/2), 
                                                 vjust = 1), 
                      axis.text.y = element_text(margin = margin(r = 0.8*5.5/2),
                                                 hjust = 1),
                      axis.ticks = element_line(colour = "grey20"), 
                      axis.ticks.length = unit(5.5/2, "pt"), 
                      axis.title.x = element_text(margin = margin(t = 0.8 * 5.5,
                                                                  b = 0.8 * 5.5/2)),
                      axis.title.y = element_text(angle = 90, 
                                                  margin = margin(r = 0.8 * 5.5,
                                                                  l = 0.8 * 5.5/2)),
                      
                      legend.background = element_rect(colour = NA), 
                      legend.spacing = unit(0.2, "cm"), 
                      legend.key = element_rect(fill = "grey95", colour = "white"),
                      legend.key.size = unit(1.2, "lines"), 
                      legend.key.height = NULL,
                      legend.key.width = NULL, 
                      legend.text = element_text(size = rel(0.8)),
                      legend.text.align = NULL,
                      legend.title = element_text(hjust = 0), 
                      legend.title.align = NULL, 
                      legend.position = "right", 
                      legend.direction = NULL,
                      legend.justification = "center", 
                      legend.box = NULL, 
                      
                      panel.background = element_rect(fill = "white"),
                      panel.grid.major = element_line(colour = "grey", size=0.5, linetype="dashed"),
                      panel.border = element_rect(fill=NA, color="grey", size=0.5, linetype="solid"),
                      panel.grid.minor = element_line(size = 0.25, linetype = 'solid', colour = "white"),
                      panel.spacing = unit(5.5, "pt"), panel.margin.x = NULL, 
                      panel.spacing.y = NULL, panel.ontop = FALSE, 
                      
                      strip.background = element_rect(fill = "grey85", colour = NA),
                      strip.text = element_text(colour = "grey10", size = rel(0.8)),
                      strip.text.x = element_text(margin = margin(t = 5.5,
                                                                  b = 5.5)), 
                      strip.text.y = element_text(angle = -90, 
                                                  margin = margin(l = 5.5, 
                                                                  r = 5.5)),
                      strip.switch.pad.grid = unit(0.1, "cm"),
                      strip.switch.pad.wrap = unit(0.1, "cm"), 
                      
                      plot.background = element_rect(colour = "white"), 
                      plot.title = element_text(size = rel(1.2), 
                                                margin = margin(b = 5.5 * 1.2)),
                      plot.margin = margin(5.5, 5.5, 5.5, 5.5),
                      complete = TRUE)

# The Rest of our plots will use this theme
theme_set(custom_theme)

ggplot(p.aq, aes(x=Date, y=value, color = name)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set1") +
  xlab("Date") +
  ylab("Wind: mph / Temp: degrees F") +
  labs(color = "Measurement", caption = "Source: National Weather Service") +
  ggtitle("Observed Wind and Temperature", subtitle = "New York City 1973")

Fixing Axis Labels

ggplot(starwars, aes(x = species, y = height, fill = species)) + 
  geom_boxplot() +
  theme(axis.text.x = element_text(angle = 60, hjust = 1)) +
  guides(fill = FALSE) +
  xlab("Species") +
  ylab("Height") +
  ggtitle("Distribution of Height by Species", subtitle = "of Star Wars Characters")
## Warning: Removed 6 rows containing non-finite values (stat_boxplot).

Facet wrapping

ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density(alpha = .6) +
  scale_fill_brewer(palette = "Dark2") +
  facet_wrap("Species") + 
  xlab("Sepal Length") +
  ylab("Density") +
  ggtitle("Density of Sepal Length by Species")

  guides(fill = FALSE)
## $fill
## [1] FALSE
## 
## attr(,"class")
## [1] "guides"

Add a horizontal line

ggplot(mtcars, aes(x = hp, y = mpg)) + 
  geom_point() +
  geom_hline(yintercept = mean(mtcars$mpg), color = "red") +
  ggtitle("Car Miles per Gallon by Horse Power") +
  xlab("Horse Power") +
  ylab("MPG")

Add a vertical line

ggplot(mtcars, aes(x = hp, y = mpg)) + 
  geom_point() +
  geom_vline(xintercept = mean(mtcars$hp), color = "blue") +
  ggtitle("Car Miles per Gallon by Horse Power") +
  xlab("Horse Power") +
  ylab("MPG")

Add Labels to Line

ggplot(mtcars, aes(x = hp, y = mpg)) + 
  geom_point() +
  geom_text(aes(x = 0, y = mean(mtcars$mpg), label = "MPG Average"), color = "red", vjust = 0, hjust = 0) +
  geom_hline(yintercept = mean(mtcars$mpg), color = "red") +
  geom_text(aes(x = mean(mtcars$hp), y = 0,  label = "HP Average"), color = "blue", angle = 90, vjust = 0, hjust = 0) +
  geom_vline(xintercept = mean(mtcars$hp), color = "blue") +
  ggtitle("Car Miles per Gallon by Horse Power") +
  xlab("Horse Power") +
  ylab("MPG")
## Warning: Use of `mtcars$mpg` is discouraged. Use `mpg` instead.
## Warning: Use of `mtcars$hp` is discouraged. Use `hp` instead.

Alternative Line Labels

lines <- mtcars %>%
  summarise(mpg = mean(mpg), hp = mean(hp))
  
ggplot(mtcars, aes(x = hp, y = mpg)) + 
  geom_point() +
  geom_hline(data = lines, aes(yintercept = mpg, color = "MPG"), show.legend = T) +
  geom_vline(data = lines, aes(xintercept = hp, color = "HP"), show.legend = T) +
  ggtitle("Car Miles per Gallon by Horse Power") +
  xlab("Horse Power") +
  ylab("MPG") +
  labs(color = "Averages")

Add Labels to Points

ggplot(mtcars, aes(x = hp, y = mpg)) + 
  geom_point() + 
  geom_text(aes(label = rowname), angle = 45, hjust = 0, vjust = 1, nudge_y = .5)

Plotly

Make it interactive!

ggplotly(ggplot(mtcars, aes(x = hp, y = mpg)) + 
  geom_point() + 
  geom_smooth() +
  ggtitle("Car Miles per Gallon by Horse Power") +
  xlab("Horse Power") +
  ylab("MPG")
)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Tooltips

# Text will format HTML automatically
ggplotly(ggplot(mtcars, aes(x = hp, y = mpg, text = paste("<b>", rowname, "</b><br>",
                                                          "MPG:", mpg,
                                                          "<br>Horse Power:", hp))) + 
  geom_point() + 
  ggtitle("Car Miles per Gallon by Horse Power") +
  xlab("Horse Power") +
  ylab("MPG"),
  tooltip = "text"
)

Resources: